﻿using System;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyBook66;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace Fb2Export
{
    public partial class toolbox : Form
    {
        MyBookInterface ifc;
        public toolbox()
        {
            this.InitializeComponent();
        }

        private void toolbox_Load(object sender, EventArgs e)
        {
            fbd.SelectedPath = Path.GetDirectoryName(Application.ExecutablePath);
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string fn = String.Format("{0}\\pim.mdb", fbd.SelectedPath);
                if (File.Exists(fn))
                {
                    ifc = new MyBookInterface(fn);
                    #region 填充列表
          
                    using (DataTable dt = ifc.MyBook66Structure)
                    {
                        tv.Nodes.Clear();
                        string k = "";
                        TreeNode tn = new TreeNode();
                        foreach (DataRow item in dt.Rows)
                        {
                            if (item["groupid"].ToString() != k)
                            {
                                tn = new TreeNode() { Text = item["groupname"].ToString(), Tag = item["groupid"].ToString() };
                                tv.Nodes.Add(tn);
                                k = item["groupid"].ToString();
                            }
                            tn.Nodes.Add(new TreeNode() { Tag = item["NovelID"].ToString(), Text = item["NovelName"].ToString() });
                        }
                    #endregion
                    }
                    selectNew();
                }
                else
                {
                    MessageBox.Show("未能获取数据库,请检查路径");
                    Application.Exit();
                }
            }
            else
            {
                Application.Exit();
            }
        }

        private void toolbox_Shown(object sender, EventArgs e)
        {

        }
        #region  选择
        
        public void selectNew()
        {

            using (DataTable dt = ifc.UnReadBooks)
            {
                selectNone();
                foreach (DataRow item in dt.Rows)
                {
                    foreach (TreeNode d in tv.Nodes)
                    {
                        if (d.Tag.ToString() == item["groupid"].ToString())
                        {
                            foreach (TreeNode x in d.Nodes)
                            {
                                if (x.Tag.ToString() == item["novelid"].ToString())
                                {
                                    x.Checked = true;
                                    break;
                                }

                            }

                            break;
                        }
                    }
                }
            }
            sNone.Checked = false;
            sNew.Checked = true;
            sAll.Checked = false;

        }
        public void selectNone()
        {
            sNone.Checked = true;
            sNew.Checked = false;
            sAll.Checked = false;
            foreach (TreeNode item in tv.Nodes)
            {
                item.Checked = false;
            }
        }
        public void selectAll()
        {
            sNone.Checked = false;
            sNew.Checked = false;
            sAll.Checked = true;
            foreach (TreeNode item in tv.Nodes)
            {
                item.Checked = true;
            }
        }
        private void tv_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count != 0)
            {
                foreach (TreeNode item in e.Node.Nodes)
                {
                    item.Checked = e.Node.Checked;
                }
            }
        }

        private void sNew_Click(object sender, EventArgs e)
        {
            selectNew();
        }

        private void sAll_Click(object sender, EventArgs e)
        {
            selectAll();
        }

        private void sNone_Click(object sender, EventArgs e)
        {
            selectNone();
        }

        #endregion
        private void bFB2_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs;
                MemoryStream mf;
                foreach (TreeNode itxem in tv.Nodes)
                {
                    foreach (TreeNode item in itxem.Nodes)
                    {
                        
                    
                    if (item.Checked == true)
                    {
                        mf = ifc.getBookFB2(item.Tag.ToString());
                        fs = File.Create(string.Format("{1}\\{0}.fb2", item.Text, fbd.SelectedPath));
                        
                        mf.WriteTo(fs);
                        fs.Flush();
                        fs.Close();
                        fs = null;
                        mf = null;
                        ifc.setRead(item.Tag.ToString());
                        
                    }
                    }
                }
            }
        }

        private void bFBZ_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs;
                MemoryStream mf,mfo;
                byte[] buf;
                foreach (TreeNode itxem in tv.Nodes)
                {
                    foreach (TreeNode item in itxem.Nodes)
                    {
                        
                    
                    if (item.Checked == true)
                    {
                        mf = ifc.getBookFB2(item.Tag.ToString());
                       
                        mfo = new MemoryStream();
                        ZipOutputStream stm = new ZipOutputStream(mfo);
                        stm.IsStreamOwner = false;
                        stm.SetLevel(9);
                        buf = mf.GetBuffer();
                        ZipEntry et = new ZipEntry(string.Format("{0}.fb2", item.Text)) { DateTime = DateTime.Now, Size = buf.Length };
                        stm.PutNextEntry(et);
                        stm.Write(buf, 0, buf.Length);
                        stm.Finish();
                        stm.Close();

                        
                        fs = File.Create(string.Format("{1}\\{0}.fbz", item.Text, fbd.SelectedPath));
                        mfo.WriteTo(fs);
                        fs.Flush();
                        fs.Close();
                        fs.Dispose();
                        fs = null;
                        mf.Dispose();
                        mf = null;
                        mfo.Dispose();
                        mfo = null;
                       if (ckAutoRead.Checked)  ifc.setRead(item.Tag.ToString());
                       stm.Dispose();
                        stm = null;
                    }
                    }
                }
                MessageBox.Show("全部生成完毕!");
            }
        }

        private void 生成后自动退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
          //  Application.Exit();
        }
        public void checkQuit()
        {

        }
    }
}
